{
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.6-final"
  },
  "orig_nbformat": 2,
  "kernelspec": {
   "name": "python37764bit5d211478a4cf4a3d93a2c891f6a9207d",
   "display_name": "Python 3.7.7 64-bit"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2,
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "'''\n",
    "字典中使用循环的技巧:\n",
    "1.在字典循环时， 使用items()可以同时取出 键 和 值\n",
    "2.在列表循环时，使用 enumerate([list])可以同时取出 索引 和 元素\n",
    "3.在2个和多个列表中循环时， 可以用zip()函数将其中的元素 一一匹配\n",
    "4.逆向循环一个序列时，可以正向先定位，在调用reversed()函数\n",
    "5.如果想按指定顺序循环一个序列时，可以调用sorted()函数，可以在不改动原序列的基础上返回一个新的排好序的序列\n",
    "6.在循环时如果需要修改列表内容，一般来说创建一个新序列会相对安全\n",
    "'''\n",
    "\n",
    "lpl = {'TOP': 'knight', 'JDG': 'Yagao', 'SN':'Angal', 'LGD': 'Xiye'}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": "TOP : knight\nJDG : Yagao\nSN : Angal\nLGD : Xiye\n"
    }
   ],
   "source": [
    "# 在字典循环时， 使用items()可以同时取出 键 和 值\n",
    "for team, mid in lpl.items():\n",
    "    print(team, ':', mid)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": "1 1\n2 4\n3 9\n"
    }
   ],
   "source": [
    "# 在列表循环时，使用 enumerate([list])可以同时取出 索引 和 元素\n",
    "for team, mid in enumerate([1,4,9]):\n",
    "    print(team+1, mid)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": "TOP jackylove\nJDG kerma\nSN Lorken\nLGD made\n"
    }
   ],
   "source": [
    "# 在2个和多个列表中循环时， 可以用zip()函数将其中的元素 一一匹配\n",
    "team = ['TOP','JDG', 'SN', 'LGD']\n",
    "ad = ['jackylove', 'kerma', 'Lorken', 'made']\n",
    "for t, a in zip(team, ad):\n",
    "    print(t, a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": "8\n7\n6\n5\n4\n3\n2\n1\n"
    }
   ],
   "source": [
    "# 逆向循环一个序列时，可以正向先定位，在调用reversed()函数\n",
    "for i in reversed(range(1,9)):\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": "jdg\nlgd\nsn\ntop\n"
    },
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "[&#39;top&#39;, &#39;jdg&#39;, &#39;sn&#39;, &#39;lgd&#39;]"
     },
     "metadata": {},
     "execution_count": 12
    }
   ],
   "source": [
    "# 如果先按指定顺序循环一个序列时，可以调用sorted()函数，可以在不改动原序列的基础上返回一个新的排好序的序列\n",
    "team = ['top', 'jdg', 'sn', 'lgd']\n",
    "for t in sorted(set(team)): # 调用set()的意义在哪里？？\n",
    "    print(t)\n",
    "team"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "[56.2, 51.7, 55.3, 52.5]"
     },
     "metadata": {},
     "execution_count": 13
    }
   ],
   "source": [
    "# 在循环时如果需要修改列表内容，一般来说创建一个新序列会相对安全\n",
    "\n",
    "import math\n",
    "\n",
    "raw_data = [56.2, float('Nan'), 51.7, 55.3, 52.5, float('Nan')]\n",
    "new_data =[]\n",
    "for value in raw_data:\n",
    "    if not math.isnan(value):\n",
    "        new_data.append(value)\n",
    "new_data"
   ]
  }
 ]
}